package bth.reconfs;

import java.util.Calendar;
import java.util.Locale;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity{
	private ViewPager Tab;
	private TabPagerAdapter TabAdapter;
	private ActionBar actionBar;
	public static final int NB_TABS = 2;
	public static final int NB_MAX_LIST = 10;
	private String[][][] planningWeek = new String[NB_TABS][NB_MAX_LIST][2];

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//if( isOnline(this) )
		//{
			planningWeek[0][0][0] = "Monday 25th august";
			planningWeek[0][0][1] = "Doctoral Symposium, Workshops, Tutorials";
			planningWeek[0][1][0] = "Tuesday 26th august";
			planningWeek[0][1][1] = "Workshops, Tutorials";
			planningWeek[0][2][0] = "Wednesday 27th august";
			planningWeek[0][2][1] = "Invited Talk: Tony Wasserman, Research/Industry Tracks, Most Influential Paper and Posters Lightning Round";
			planningWeek[0][3][0] = "Thursday 28th august";
			planningWeek[0][3][1] = "Invited Talk: Annie I. Antón, Research/Industry Tracks, Industry Laboratory, Panels & Debates, Poster/Demo Sessions";
			planningWeek[0][4][0] = "Friday 29th august";
			planningWeek[0][4][1] = "Invited Talk: Anthony Finkelstein, Research/Industry Tracks, Panels & Debates, Workshop highlights, Mini-Tutorial";
			
			planningWeek[1][0][0] = "RE'14 - Student Volunteers";
			planningWeek[1][0][1] = "Mon Sep 01 2014";
			planningWeek[1][1][0] = "RE'14 - RE4SuSy Karlskrona Manifesto";
			planningWeek[1][1][1] = "Fri Aug 29 2014";
			planningWeek[1][2][0] = "RE'14 - Industry Collaboration Idea";
			planningWeek[1][2][1] = "Thu Aug 28 2014";
			planningWeek[1][3][0] = "RE'14 - Speed dating";
			planningWeek[1][3][1] = "Fri Aug 22 2014";
			planningWeek[1][4][0] = "RE'14 - Train ticket booking working again";
			planningWeek[1][4][1] = "Fri Aug 22 2014";
			planningWeek[1][5][0] = "RE'14 - Train ticket booking issues";
			planningWeek[1][5][1] = "Thu Aug 21 2014";
			planningWeek[1][6][0] = "RE'14 - Event calendar";
			planningWeek[1][6][1] = "Wed Aug 13 2014";
			createView();

			//scheduleAlarm(this);
		/*} else {
			Toast.makeText(getApplicationContext(), "Vous devez être connecté à internet", Toast.LENGTH_SHORT).show();
		}*/
	}

	public static void scheduleAlarm(Context context)
	{
		// Create the Intent for launch the notification actions
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

		// Choose the date you want to notifie the user
		Calendar timeRepeating = Calendar.getInstance();
		timeRepeating.set(Calendar.HOUR_OF_DAY, 13);
		timeRepeating.set(Calendar.MINUTE, 0);
		timeRepeating.set(Calendar.SECOND, 0);

		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

		//set the alarm for particular time + AlarmManager.INTERVAL_DAY
		//alarmManager.set(AlarmManager.RTC_WAKEUP, new GregorianCalendar().getTimeInMillis()+80000, PendingIntent.getBroadcast(this, 1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeRepeating.getTimeInMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pendingIntent);
	}

	@SuppressWarnings("deprecation")
	public void createView() {
		TabAdapter = new TabPagerAdapter(getSupportFragmentManager(), planningWeek);

		Tab = (ViewPager)findViewById(R.id.pager);
		Tab.setOnPageChangeListener(
				new ViewPager.SimpleOnPageChangeListener() {
					public void onPageSelected(int position) {

						actionBar = getSupportActionBar();
						actionBar.setSelectedNavigationItem(position);
					}
				});
		Tab.setAdapter(TabAdapter);

		actionBar = getSupportActionBar();
		//Enable Tabs on Action Bar
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		ActionBar.TabListener tabListener = new ActionBar.TabListener(){

			public void onTabReselected(
					android.support.v7.app.ActionBar.Tab tab,
					android.support.v4.app.FragmentTransaction ft) {}

			public void onTabSelected(
					android.support.v7.app.ActionBar.Tab tab,
					android.support.v4.app.FragmentTransaction ft) {
				Tab.setCurrentItem(tab.getPosition());
			}

			public void onTabUnselected(
					android.support.v7.app.ActionBar.Tab tab,
					android.support.v4.app.FragmentTransaction ft) {}
		};
		//Add New Tabs
		actionBar.addTab(actionBar.newTab().setText("Schedule").setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText("News").setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText("General").setTabListener(tabListener));
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.browser, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		final int id = item.getItemId();
		switch (id) {
		case R.id.show_conf_website_in_browser:
			String uri = String.format(Locale.ENGLISH, "http://webhotel.bth.se/re14/");//latitude, longitude, zoom, label
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
			this.startActivity(intent);
			return true;
		}
		return false;
	}

	/* Verifie si le telephone est connecte a internet */
	public static boolean isOnline(Context context)
	{
		ConnectivityManager connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) || (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ 
			return true;
		}
		return false;
	}
}